<?php 

class News_model extends CI_Model {

	function __construct(){
		parent::__construct();
		$this->load->database();
	}

	public function get_upd_data($id){
		
		$query = $this->db->get_where('news', array('id' => $id));
		return $query->row_array();
	}

	public function get_new($p_id){
		$this->db->order_by('datetime', 'desc');
		$query = $this->db->get_where('news', array('p_id' => $p_id));
		return $query->result_array();
	}

	public function insert($data){
		//$data['content'] = str_replace(' ', '&nbsp;', $data['content']);
		//$data['content'] = nl2br($data['content']);
		$insert = array(
			'title' => addslashes($data['title']),
			'json_data' => json_encode($data),
			'datetime' => $data['datetime'],
			'type' => addslashes($data['type']),
			'p_id' => $data['p_id'],
		);
		$query = $this->db->insert('news', $insert);
		return $this->db->affected_rows();
	}

	public function update($data, $id){
		//$data['content'] = str_replace(' ', '&nbsp;', $data['content']);
		//$data['content'] = nl2br($data['content']);
		$update = array(
			'title' => addslashes($data['title']),
			'json_data' => json_encode($data),
			'datetime' => $data['datetime'],
			'type' => addslashes($data['type']),
			'p_id' => $data['p_id'],
		);
		$this->db->where('id', $id);
		$query = $this->db->update('news', $update);
		return $this->db->affected_rows();
	}

	public function deletenews($id){
		$this->db->where('id', $id);
		$query = $this->db->delete('news');
		return $query->result_array();
	}


}
